<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Area of Circle</title>
<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../fp_eg.html" title="Examples">
<link rel="prev" href="floatbuiltinctor.html" title="Construction from Specific Values Without Precision Loss">
<link rel="next" href="caveats.html" title="Drop-in Caveats">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="floatbuiltinctor.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="caveats.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="boost_multiprecision.tut.floats.fp_eg.aos"></a><a class="link" href="aos.html" title="Area of Circle">Area of
          Circle</a>
</h5></div></div></div>
<p>
            Generic numeric programming employs templates to use the same code for
            different floating-point types and functions. Consider the area of a
            circle a of radius r, given by
          </p>
<div class="blockquote"><blockquote class="blockquote"><p>
              <span class="emphasis"><em>a = π * r<sup>2</sup></em></span>
            </p></blockquote></div>
<p>
            The area of a circle can be computed in generic programming using Boost.Math
            for the constant π as shown below:
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">area_of_a_circle</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">r</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special">;</span>
   <span class="keyword">return</span> <span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span> <span class="special">*</span> <span class="identifier">r</span> <span class="special">*</span> <span class="identifier">r</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            It is possible to use <code class="computeroutput"><span class="identifier">area_of_a_circle</span><span class="special">()</span></code> with built-in floating-point types
            as well as floating-point types from Boost.Multiprecision. In particular,
            consider a system with 4-byte single-precision float, 8-byte double-precision
            double and also the <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>
            data type from Boost.Multiprecision with 50 decimal digits of precision.
          </p>
<p>
            We can compute and print the approximate area of a circle with radius
            123/100 for <code class="computeroutput"><span class="keyword">float</span></code>, <code class="computeroutput"><span class="keyword">double</span></code> and <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>
            with the program below (see next section for choosing 123/100 instead
            of 1.23).
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_dec_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float_50</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">**)</span>
<span class="special">{</span>
   <span class="keyword">const</span> <span class="keyword">float</span> <span class="identifier">r_f</span><span class="special">(</span><span class="keyword">float</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">/</span> <span class="number">100</span><span class="special">);</span>
   <span class="keyword">const</span> <span class="keyword">float</span> <span class="identifier">a_f</span> <span class="special">=</span> <span class="identifier">area_of_a_circle</span><span class="special">(</span><span class="identifier">r_f</span><span class="special">);</span>

   <span class="keyword">const</span> <span class="keyword">double</span> <span class="identifier">r_d</span><span class="special">(</span><span class="keyword">double</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">/</span> <span class="number">100</span><span class="special">);</span>
   <span class="keyword">const</span> <span class="keyword">double</span> <span class="identifier">a_d</span> <span class="special">=</span> <span class="identifier">area_of_a_circle</span><span class="special">(</span><span class="identifier">r_d</span><span class="special">);</span>

   <span class="keyword">const</span> <span class="identifier">cpp_dec_float_50</span> <span class="identifier">r_mp</span><span class="special">(</span><span class="identifier">cpp_dec_float_50</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">/</span> <span class="number">100</span><span class="special">);</span>
   <span class="keyword">const</span> <span class="identifier">cpp_dec_float_50</span> <span class="identifier">a_mp</span> <span class="special">=</span> <span class="identifier">area_of_a_circle</span><span class="special">(</span><span class="identifier">r_mp</span><span class="special">);</span>

   <span class="comment">// 4.75292</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">a_f</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

   <span class="comment">// 4.752915525616</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">a_d</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

   <span class="comment">// 4.7529155256159981904701331745635599135018975843146</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">cpp_dec_float_50</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">a_mp</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            In later examples we'll look at calling both standard library and Boost.Math
            functions from within generic code. We'll also show how to cope with
            template arguments which are expression-templates rather than number
            types.
          </p>
<p>
            But first some warnings about how multiprecision types are slightly but
            significantly different <a href="https://en.cppreference.com/w/cpp/language/types" target="_top">fundamental
            (built-in) types</a>.
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="floatbuiltinctor.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="caveats.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
